package com.ruoyi.project.mall.service.impl;

import cc.openkit.common.KitUtil;
import com.ruoyi.common.utils.bootdo.MD5Utils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.mall.dao.MallCompanyUserDao;
import com.ruoyi.project.mall.domain.MallCompanyUserDO;
import com.ruoyi.project.mall.service.MallCompanyUserService;
import com.ruoyi.project.wx.dao.MemberCodeDao;
import com.ruoyi.project.wx.dao.MemberUserDao;
import com.ruoyi.project.wx.domain.MemberCodeDO;
import com.ruoyi.project.wx.domain.MemberUserDO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class MallCompanyUserServiceImpl implements MallCompanyUserService {
	@Autowired
	private MallCompanyUserDao mallCompanyUserDao;
	@Autowired
	private MemberCodeDao memberCodeDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public MallCompanyUserDO get(String id){
		return mallCompanyUserDao.get(id);
	}
	
	@Override
	public List<MallCompanyUserDO> list(Map<String, Object> map){
		return mallCompanyUserDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return mallCompanyUserDao.count(map);
	}
	
	@Override
	public int save(MallCompanyUserDO mallCompanyUser){
		return mallCompanyUserDao.save(mallCompanyUser);
	}
	
	@Override
	public int update(MallCompanyUserDO mallCompanyUser){
		return mallCompanyUserDao.update(mallCompanyUser);
	}
	
	@Override
	public int remove(String id){
		return mallCompanyUserDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return mallCompanyUserDao.batchRemove(ids);
	}

    @Override
    public List<MallCompanyUserDO> likeList(Map<String, Object> map){
        return mallCompanyUserDao.likeList(map);
    }
    @Override
    public int likeListCount(Map<String, Object> map){
        return mallCompanyUserDao.likeListCount(map);
    }

    @Override
    public R companyUserRegister(Map<String, Object> params) {
		try {
			//查看验证码
			params.put("delFlag",0);
			List<MemberCodeDO> list = memberCodeDao.list(params);
			if(list.size()<=0){
				return R.error("验证码有误");
			}
			//查看手机号
			MemberUserDO memberUserDO = memberUserDao.findByPhone(params.get("phone").toString());
			if(memberUserDO!=null){
				return R.error("手机号已注册");
			}else{

				memberUserDO = new MemberUserDO();
				memberUserDO.setMoney(new BigDecimal("0.00"));
				memberUserDO.setId(UuidUtil.get32UUID());
				memberUserDO.setFreeze(new BigDecimal("0.00"));
				memberUserDO.setDisabled(0);
				memberUserDO.setDelFlag(0);
				memberUserDO.setSex(0);
				memberUserDO.setCreateAt(new Date());
				memberUserDO.setName("昵称"+ KitUtil.getStringRandom(8));
				memberUserDO.setAvator("https://tse2-mm.cn.bing.net/th/id/OIP.IykEwu6UUNOvq9LFU0d3kwAAAA?w=164&h=180&c=7&o=5&dpr=1.25&pid=1.7");
				memberUserDO.setLevel(2);
				String encrypt = MD5Utils.encrypt(params.get("phone").toString(), params.get("password").toString());
				memberUserDO.setPassword(encrypt);
				memberUserDO.setPhone(params.get("phone").toString());
				memberUserDO.setUpdateAt(new Date());

				MallCompanyUserDO mallCompanyUserDO = new MallCompanyUserDO();
				BeanUtils.populate(mallCompanyUserDO,params);
				mallCompanyUserDO.setCaAt(new Date());
				mallCompanyUserDO.setDelFlag(0);
				mallCompanyUserDO.setId(UuidUtil.get32UUID());
				mallCompanyUserDO.setLevel(0);
				mallCompanyUserDO.setUpAt(new Date());
				mallCompanyUserDO.setParentId("0");
				mallCompanyUserDO.setUserId(memberUserDO.getId());
				String encrypt1 = MD5Utils.encrypt(params.get("phone").toString(), "123456");
				mallCompanyUserDO.setPassword(encrypt1);
				mallCompanyUserDO.setCreateId(memberUserDO.getId());
				mallCompanyUserDao.save(mallCompanyUserDO);
				memberUserDao.save(memberUserDO);


				return R.ok();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}

	}

    @Override
    public MallCompanyUserDO getByUserId(String userId) {
		return  mallCompanyUserDao.getByUserId(userId);
    }

	@Override
	public R completionCompanyUser(Map<String, Object> params) {
		try {
			MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
			memberUserDO.setName(params.get("userName").toString());
			memberUserDO.setSex(Integer.parseInt(params.get("sex").toString()));
			memberUserDO.setAvator(params.get("avator").toString());
			memberUserDO.setUpdateAt(new Date());
			memberUserDao.update(memberUserDO);
			MallCompanyUserDO mallCompanyUserDO = mallCompanyUserDao.getByUserId(params.get("userId").toString());
			if(mallCompanyUserDO==null){
				return R.error("信息有误");
			}else{
				BeanUtils.populate(mallCompanyUserDO,params);
				mallCompanyUserDO.setUpAt(new Date());
				mallCompanyUserDao.update(mallCompanyUserDO);
				return R.ok();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
